package com.androidserver.server;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.androidserver.util.DatastoreWrapper;

@SuppressWarnings("serial")
public class LoginRegister extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
	throws IOException {
		
		if (!req.getHeader("User-Agent").contains("Android")) {
			resp.getWriter().print("No browser access");
			return;
		}
		DatastoreWrapper dataWrapper = DatastoreWrapper.getInstance();
		if (req.getParameter("action").equals("login")) {
			String result = dataWrapper.validateMember(req.getParameter("memberName"), req.getParameter("password"));
			if (!result.equals("FAIL")) {
				resp.getWriter().print("OK.ID=" + result);
			} else {
				resp.getWriter().print("FAIL");
			}
		} else if (req.getParameter("action").equals("register")) {
			String result = dataWrapper.addMember(req.getParameter("memberName"), req.getParameter("password"));
			if (!result.equals("FAIL")) {
				resp.getWriter().print("OK.ID=" + result);
			} else {
				resp.getWriter().print("FAIL");
			}
		} else if (req.getParameter("action").equals("changeName")) {
			if (dataWrapper.changeMemberName(req.getParameter("idCode"), req.getParameter("newName"))) {
				resp.getWriter().print("OK");
			} else {
				resp.getWriter().print("FAIL");
			}
		} else if (req.getParameter("action").equals("changePassword")) {
			if (dataWrapper.changeMemberPassword(req.getParameter("idCode"),
					req.getParameter("oldPw"), req.getParameter("newPw"))) {
				resp.getWriter().print("OK");
			} else {
				resp.getWriter().print("FAIL");
			}
		}
		
	}
}
